package hr.fer.pus.dll_will.misc;

import hr.fer.pus.dll_will.sherlock.Base64Coder;
import hr.fer.pus.dll_will.sherlock.Security;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

public class SecurityTest {

	/**
	 * @param args
	 * @throws NoSuchAlgorithmException 
	 * @throws NoSuchPaddingException 
	 */
	public static void main(String[] args) throws Exception {
		KeyPairGenerator gen;
		gen = KeyPairGenerator.getInstance("RSA");
		gen.initialize(1024);
		KeyPair pair = gen.generateKeyPair();
		RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate();
		RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic();
		
		String privExp = privateKey.getPrivateExponent().toString();
		String publExp = publicKey.getPublicExponent().toString();
		String modulus = publicKey.getModulus().toString();
		
		
		
		String privateKey1 = modulus+" "+privExp;
		String publicKey1 = modulus+" "+publExp;
		
		String plain = "Hello World!";
		

		Cipher cip = Cipher.getInstance("RSA");
		cip.init(Cipher.ENCRYPT_MODE, publicKey);
		byte[] ciphered = cip.doFinal(plain.getBytes());
		cip.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] plained = cip.doFinal(ciphered);
		
		
		String coded	= Security.encrypt(plain, publicKey1);
		String decoded 	= Security.decrypt(coded, privateKey1);
		
		
		System.out.println(new String(plained));
		System.out.println(decoded);
		
		
	}

}
